class Article < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :comments
  
  has_many :taggings#, :dependent => :destory
  has_many :tags, :through => :taggings
  
  validates_presence_of :title, :message => "은 빈칸일 수 없습니다."
  
  def before_save
    self.body_html = BlueCloth.new(self.body).to_html
  end
  
  def after_save
    self.taggings.destroy_all
    
    self.tag_list.split(",").each do |str|
      str.strip!
      
      unless (tag = Tag.find_by_name(str))
        tag = Tag.create(:name => str)
      end
      
      self.tags << tag
    end
  end
end
